//
//  GYFloatingWindow.m
//  DriverApp
//
//  Created by zgy on 2025/4/21.
//  Copyright © 2025 LS-LONG. All rights reserved.
//

#import "GYFloatingWindow.h"
#import "GYFloatingView.h"


@implementation GYFloatingWindow
{
    GYFloatingView *_floatingView;
}
+ (instancetype)sharedInstance {
    static GYFloatingWindow *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[GYFloatingWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    });
    return sharedInstance;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.windowLevel = UIWindowLevelAlert + 1; // 确保显示在最前面
        self.backgroundColor = [UIColor clearColor];
        self.hidden = NO;
        
        // 设置 rootViewController
        self.rootViewController = [UIViewController new];
        
        // 添加悬浮视图
        _floatingView = [[GYFloatingView alloc] initWithFrame:CGRectMake(100, 200, 60, 30)];
        [self addSubview:_floatingView];
    }
    return self;
}


// 重写 hitTest 方法
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    // 将触摸点转换到悬浮视图的坐标系中
    CGPoint convertedPoint = [self convertPoint:point toView:_floatingView];

    // 判断触摸点是否在悬浮视图中
    if ([_floatingView pointInside:convertedPoint withEvent:event]) {
        return [_floatingView hitTest:convertedPoint withEvent:event]; // 将事件传递给悬浮视图
    }

    // 否则将事件传递给下层页面
    return nil;
}



@end
